package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {

    private StringBuilder input;
    private TextView inputView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_calculator);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.num0).setOnClickListener(this);
        findViewById(R.id.num1).setOnClickListener(this);
        findViewById(R.id.num2).setOnClickListener(this);
        findViewById(R.id.num3).setOnClickListener(this);
        findViewById(R.id.num4).setOnClickListener(this);
        findViewById(R.id.num5).setOnClickListener(this);
        findViewById(R.id.num6).setOnClickListener(this);
        findViewById(R.id.num7).setOnClickListener(this);
        findViewById(R.id.num8).setOnClickListener(this);
        findViewById(R.id.num9).setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.subtraction).setOnClickListener(this);
        findViewById(R.id.multiplication).setOnClickListener(this);
        findViewById(R.id.division).setOnClickListener(this);

        TextView output = findViewById(R.id.output);

        inputView = findViewById(R.id.input);

        findViewById(R.id.equals).setOnClickListener(v->{

            // 计算结果
            output.setText("得到计算结果");
        });

        findViewById(R.id.clear).setOnClickListener(v->{

            this.input = new StringBuilder();
            output.setText("");
        });

        input = new StringBuilder();
    }

    @Override
    public void onClick(View view) {

        CharSequence text = ((Button) view).getText();
        input.append(text);

        inputView.setText(input.toString());
    }
}